package 文件io;
import java.io.*;
/*
  字节缓存流  提高效率
 BufferedOutputStream  new BufferedOutputStream(字节输出流流对象)
    >默认缓存大小是8k，可手动指定，每次写入会把数据写入byte缓存数组中，当数组写满后会把
	>数据写入文件，并且把数组的下标归零(数组未清空)，继续缓存数据依次覆盖byte数组数据
 BufferedInputStream  new BufferedInputStream(字节输入流对象)
    >默认缓存大小也是8k
 
 
*/
public class 字节缓冲流
{
	public static void main (String[] args){
		byteWriter();
		byteReader();
	}
	private static void byteWriter(){
		File file=new File(FilePath.PATH+"/test2.txt");
		if(!file.exists()){
			try
			{
				file.createNewFile();
			}
			catch (IOException e)
			{}
		}
		try
		{
			OutputStream out=new FileOutputStream(file);//这里没写append为true，为覆盖模式
			BufferedOutputStream bos=new BufferedOutputStream(out);
			String s="哈哈哈我被写入了ll";
			bos.write(s.getBytes());
			bos.close();
			out.close();//1.7以后，bos.close()里面会自动关闭out的close()
		}
		catch (FileNotFoundException e)
		{}
		catch(IOException e){}

	}
	private static void byteReader(){
		File file=new File(FilePath.PATH+"/test2.txt");
		try
		{
			InputStream in= new FileInputStream(file);
			BufferedInputStream bis=new BufferedInputStream(in);
			byte[] bytes=new byte[1024];
			int len=-1;
			while((len=bis.read(bytes))!=-1){
				System.out.println(new String(bytes,0,len));
			}
			bis.close();
		}
		catch (FileNotFoundException e)
		{}
		catch(IOException e){}

	}
	/*1.7 try新写法不用关闭close了
	private static void byteReader2(){
		File file=new File(FilePath.PATH+"/test2.txt");
		
		try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file)))
		{

			byte[] bytes=new byte[1024];
			int len=-1;
			while ((len = bis.read(bytes)) != -1)
			{
				System.out.println(new String(bytes, 0, len));
			}
		}catch(IOException e){}
		catch(FileNotFoundException e){}
	}
	*/
}
